import { _decorator, Collider, Component, EventTouch, Input, input, ITriggerEvent, Label, Node, ParticleSystem, RigidBody, v3, Vec3 } from 'cc';
import { joystick } from './joystick';
import { globalConfig } from './globalConfig';
import { createFood } from './createFood';
const { ccclass, property } = _decorator;

@ccclass('playerController')
export class playerController extends Component {
    @property({ type: Number }) speed: number = 0.1;
    @property(joystick) joystick: joystick
    @property(createFood) createFood:createFood
    @property(Label) scope:Label
    @property(Node) particleNode:Node


    rigibody: RigidBody
    collider: Collider
    particle: ParticleSystem

    start() {
        this.rigibody = this.node.getComponent(RigidBody)
        this.collider = this.node.getComponent(Collider)
        this.particle = this.particleNode.getComponent(ParticleSystem)
        this.collider.on("onTriggerStay", this.onTriggerStay, this)
    }

    update(deltaTime: number) {
        let v = new Vec3(this.joystick.getDir().x, this.joystick.getDir().y, this.joystick.getDir().z)
        this.rigibody.applyForce(v.multiplyScalar(0.03))
    }

    onTriggerStay(event: ITriggerEvent) {
        event.otherCollider.node.destroy()
        globalConfig.score++
        this.scope.string = globalConfig.score.toString()
        this.particle.capacity = globalConfig.score
        globalConfig.foodNumber--
        if(globalConfig.foodNumber<=0){
            this.createFood.createFood()
        }
    }
}


